<!-- 2017-12-7 14:56:03 | 修改 木遥（微信:  http://marsgis.cn/weixin.html ） -->
<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="apple-touch-fullscreen" content="yes" />
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="x5-fullscreen" content="true" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />

    <!-- 标题及搜索关键字 -->
    <meta name="keywords" content="火星科技,cesium,2D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
    <meta
      name="description"
      content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS"
    />

    <link rel="shortcut icon" type="image/x-icon" href="https://muyao1987.gitee.io/cdn/marsgis.cn/favicon/favicon.ico" />
    <title>移动端位置定位 | Mars2d | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

    <script
      type="text/javascript"
      src="../lib/include-lib.js"
      libpath="../lib/"
      include="jquery,font-awesome,bootstrap,layer,haoutil,mars2d"
    ></script>

    <link href="css/style.css" rel="stylesheet" />

    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=ae29a37307840c7ae4a785ac905927e0"></script>
  </head>

  <body class="dark">
    <!--加载前进行操作提示，优化用户体验-->
    <div id="mask" class="signmask" onclick="removeMask()"></div>

    <div id="mars2dContainer" class="mars2d-container"></div>

    <script src="./js/common.js"></script>
    <script type="text/javascript">
      "use script"; //开发环境建议开启严格模式

      var map;

      function initMap(options) {
        //合并属性参数，可覆盖config.json中的对应配置
        let mapOptions = mars2d.Util.merge(options, {});
        delete mapOptions.control;

        //创建二维地球场景
        map = new mars2d.Map("mars2dContainer", mapOptions);

        const geolocation = new Geolocation({
          insertIndex: 0,
          position: "bottomright",
        });
        map.addControl(geolocation);

        // 手动调用，开始定位  toolButton.startTracking();
        geolocation.startTracking();
      }

      // 采用高德地图定位的算法 ， 参考帮助文档：https://lbs.amap.com/api/javascript-api/guide/services/geolocation
      class Geolocation extends mars2d.control.ToolButton {
        constructor(options = {}) {
          super({
            position: "bottomleft",
            title: "GPS定位",
            ...options,
          });
        }
        _mountedHook() {
          L.DomUtil.addClass(this._container.firstChild, "mars2d-toolbar-locate");
        }
        _onClick() {
          this.startTracking();
        }

        stopTracking() {
          L.DomUtil.removeClass(this._container.firstChild, "mars2d-toolbar-locate");
          L.DomUtil.addClass(this._container.firstChild, "mars2d-toolbar-locate-loading");

          this.clearLocationPoint();
        }
        startTracking() {
          AMap.plugin("AMap.Geolocation", () => {
            L.DomUtil.removeClass(this._container.firstChild, "mars2d-toolbar-locate-loading");
            L.DomUtil.addClass(this._container.firstChild, "mars2d-toolbar-locate");

            if (!this.geolocation) {
              this.geolocation = new AMap.Geolocation({
                enableHighAccuracy: true, // 是否使用高精度定位，默认：true
                timeout: 10000, // 设置定位超时时间，默认：无穷大
                convert: true, //自动偏移坐标，偏移后的坐标为高德坐标，默认：true
              });
            }

            let that = this;
            this.geolocation.getCurrentPosition();

            function onComplete(data) {
              // data是具体的定位信息
              let wgsPoint = mars2d.PointTrans.gcj2wgs([data.position.lng, data.position.lat]);
              that.flyToLocation(L.latLng(wgsPoint[1], wgsPoint[0]));
            }

            function onError(data) {
              // 定位出错,参考：https://lbs.amap.com/faq/js-api/map-js-api/position-related
              mars2d.Util.msg(data.message, "定位失败");
            }
            AMap.event.addListener(this.geolocation, "complete", onComplete);
            AMap.event.addListener(this.geolocation, "error", onError);
          });
        }
        flyToLocation(latlng) {
          L.DomUtil.removeClass(this._container, "mars2d-toolbar-locate-loading");
          L.DomUtil.addClass(this._container, "mars2d-toolbar-locate");

          this._map.flyToPoint(latlng);

          this.clearLocationPoint();
          let graphic = new mars2d.graphic.DivLightPoint({
            latlng: latlng,
            style: {
              color: "#0000ff",
            },
            tooltip: "我的位置：" + latlng.lng + "," + latlng.lat,
          });
          this._map.graphicLayer.addGraphic(graphic);

          this.graphic = graphic;
        }
        clearLocationPoint() {
          if (!this.graphic) {
            return;
          }
          this.graphic.remove();
          this.graphic = null;
        }
      }
    </script>
  </body>
</html>
